package com.fs.controller;

import com.fs.mapper.OrderMapper;
import com.fs.pojo.GoodsCart;
import com.fs.pojo.Order;
import com.fs.service.OrderService;
import com.fs.vo.CommRequestVO;
import com.fs.vo.OrderVO;
import com.fs.vo.ResultVO;
import com.fs.wx.WeWxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;

@Api("订单管理")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/list")
    public ResultVO orderList() {
        return ResultVO.success();
    }

    @ApiOperation("立即购买-下订单")
    @PostMapping("/buy")
    public ResultVO buy(@RequestBody OrderVO orderVO) {
        return orderService.buyGoods(orderVO);
    }

    @ApiOperation("查询未付款订单信息")
    @GetMapping("/info/{orderId}")
    public ResultVO getOrderByOrderId(@PathVariable("orderId") String orderId) {
        return orderService.getOrderByOrderId(orderId);
    }

    @ApiOperation("查询订单信息")
    @GetMapping("/info2/{orderId}")
    public ResultVO getOrderByOrderId2(@PathVariable("orderId") String orderId) {
        return orderService.getOrderByOrderId2(orderId);
    }

    @ApiOperation("查询未付款订单信息-含商品列表")
    @GetMapping("/infos/{orderId}")
    public ResultVO getOrderInfosByOrderId(@PathVariable("orderId") String orderId) {
        return orderService.getOrderInfosByOrderId(orderId);
    }

    @ApiOperation("模拟支付")
    @GetMapping("/mock/{orderId}")
    public ResultVO mockOrderByOrderId(@PathVariable("orderId") String orderId) {
        return orderService.mockOrderByOrderId(orderId);
    }

    @ApiOperation("从购物车生成订单")
    @GetMapping("/cartPay")
    public ResultVO cartPay(){
        return orderService.cartPay();
    }

    @ApiOperation("订单添加收货地址、留言")
    @PutMapping("/addAddr")
    public ResultVO addAddr(@RequestBody Map<String,String> map){
        return orderService.addAddr(map );
    }

    @ApiOperation("查看全部订单")
    @GetMapping("/getAll")
    public ResultVO getAll(CommRequestVO vo){
        return orderService.getAll(vo);
    }

    @Autowired
    private WeWxPayService wxPayService;
    
    @PostMapping("/wx")
    public ResultVO wx(@RequestBody Order o)    {
        Map<String, String> map=null;
//        String orderid = String.valueOf(Math.abs(new Random().nextLong()));
        try {
            map = wxPayService.unifiedOrder(o.getOrderNo(), o.getOrderNo(), o.getPrice()+"", o.getOrderNo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.success(map);
    }

}
